/**
* @CopyRight weitaming.com
* @author wuyahui
* @E-mail wuyahui@ispied.com
* @version 1.0.0
* @Date  2014年9月3日 下午5:06:16
*/
package com.tstudy.laoshibang.base;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.utils.LogUtil;
import com.tstudy.laoshibang.utils.SharedPreferencesUtil;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
@EActivity(R.layout.guide)
public class GuideActivity extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	List<ImageView> mViewList = new ArrayList<ImageView>();
	ListItemPageAdapter mPageAdapter;
	
	@ViewById(R.id.guide_viewpager)
	public ViewPager mViewPager;
	
	@ViewById(R.id.guide_viewpager_action)
	public TextView mViewPagerAction;
	
	@AfterViews
	public void gotoMain(){
		mPageAdapter = new ListItemPageAdapter();
		mViewPager.setAdapter(mPageAdapter);
		int [] resIds = {R.drawable.guide_01,R.drawable.guide_02,R.drawable.guide_03,R.drawable.guide_04};
		for (int i = 0; i < resIds.length; i++) {
			ImageView imageView = new ImageView(BaseApplication.mContext);
			imageView.setImageResource(resIds[i]);
			imageView.setScaleType(ScaleType.FIT_XY);
			if (i == resIds.length-1) {
				imageView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						IndexActivity.show(false,true);
						finish();
					}
				});
			}
			mViewList.add(imageView);
		}
		mPageAdapter.setData(mViewList);
		SharedPreferencesUtil.setFirstOpen(false);
	}
	
	class ListItemPageAdapter extends PagerAdapter implements OnPageChangeListener{
		List<ImageView> mViewList = new ArrayList<ImageView>();
		public void setData(List<ImageView> viewList){
			mViewList = viewList;
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return mViewList.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
		
		 @Override  
		    public void destroyItem(ViewGroup container, int position,Object object) {  
		        container.removeView(mViewList.get(position));  

		    }  

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mViewList.get(position));
			return mViewList.get(position);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int position) {
			LogUtil.d("----onpageselected   position==" + position);
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			// TODO Auto-generated method stub
		}
		
	}
	@Override
	public void initArgs() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}
}
